﻿using BusinessLayer;
using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CustomerServiceCenter.Helper
{
    /// <summary>
    /// 验证上传数据的有效性
    /// </summary>
    public class AuthcationDataHelper
    {
        /// <summary>
        /// 验证上传的所有数据是否有效
        /// </summary>
        /// <param name="list">上传的对象集合</param>
        /// <returns>Dictionary，key表示上传的集合的第几行数据,value是错误字段</returns>
        public static Dictionary<int, string> CheckImportData(List<Report> list)
        {
            Dictionary<int, string> errorData = new Dictionary<int,  string>();
            if (null != list && list.Count >0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    //验证数据空值错误
                    string nullError = NullValidate(list[i]);
                    if (nullError != null)
                    {
                        SetError(i, nullError, ref errorData);
                        break;
                    }
                    //验证数据逻辑错误
                    string logicError = LogicValidate(list[i]);
                    if (logicError != null)
                    {
                        SetError(i, logicError, ref errorData);
                        break;
                    }

                }
            }
            
            return errorData;
        }

        private static string LogicValidate(Report report)
        {
            //如果
            long[] status = new long[] {0,1,2,3,4 };
            if (!status.Contains(report.Status))
            {
                return "status数据不匹配";
            }
            if (!report.Type.Trim().Equals("request"))
            {
                return "Type必须为request";
            }
            return null;
        }

        /// <summary>
        /// 设置错误列表
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="error"></param>
        private static void SetError(int key, string value,ref Dictionary<int,string> error)
        {
            if (error.ContainsKey(key))
            {
                value = error[key] + "&" + value;
                error.Remove(key);
            }
            error.Add(key, value);
        }
        /// <summary>
        /// 空值验证
        /// </summary>
        /// <param name="report"></param>
        /// <param name="error">返回有错的字段和错误信息</param>
        private static string NullValidate(Report report)
        {
            string errormsg = " Can Not Be Empty";
            //验证CustomerID
            if (report.CustomerID==0)
            {
               
                return "CustomerID" + errormsg;
            }
            else
            {
                VClientEntity client  = ClientBusiness.GetClient(report.CustomerID);
                if (null == client)
                {
                    return "CustomerID 对应的公司不存在";
                }
            }
            //验证
            if (report.ProjectID == 0)
            {
                return "ProjectID" + errormsg;
            }
            else
            {
                List<VApplication> list = ProjectBusiness.GetApplicationInfo(report.ProjectID);
                if (null == list || list.Count <= 0)
                {
                    return "ProjectID 对应的项目不存在";
                }
            }
            if (report.FirstAssignedUserID == 0 && report.SolvedUserID == 0)
            {
                return "FirstAssignedUserID OR SolvedUserID" + errormsg;
            }
            return null;
        }

    }
}